package com.ezcolour.ticket.service.impl;

import com.ezcolour.ticket.service.BaseService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public abstract class BaseServiceImpl<T> implements BaseService<T> {

    public abstract JpaRepository<T,Long> getBaseRepository();

    @Override
    public T findById(Long id) {
        Optional<T> objs = getBaseRepository().findById(id);
        return objs.get();
    }

    @Override
    public T save(T o) {
        return getBaseRepository().save(o);
    }

    @Override
    public void delete(T o) {
        getBaseRepository().delete(o);
    }

    @Override
    public void deleteById(Long id) {
        getBaseRepository().deleteById(id);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        getBaseRepository().deleteInBatch(findAllByIds(ids));
    }

    @Override
    public List findAll() {
        return getBaseRepository().findAll();
    }
    @Override
    public List findAllByIds(List<Long> ids) {
        return getBaseRepository().findAllById(ids);
    }
    @Override
    public Page<T> findAll(Pageable pageable) {
        return getBaseRepository().findAll(pageable);
    }

    @Override
    public Long count() {
        return getBaseRepository().count();
    }
}
